<?php

namespace App\Trip\Services\Plane\Airport;

use App\Models\Common\Area\Repositories\Interfaces\AreaRepositoryInterface;
use App\Models\Plane\Airports\Repositories\Interfaces\AirportRepositoryInterface;
use Illuminate\Support\Str;
use JetBrains\PhpStorm\ArrayShape;

class AirportMap
{

    private mixed $airportRepo;

    private mixed $areaRepo;

    public function __construct()
    {
        $this->airportRepo = app(AirportRepositoryInterface::class);
        $this->areaRepo    = app(AreaRepositoryInterface::class);
    }

    public function write(): void
    {
        $maps        = $this->map();
        $json_map    = $maps['json_map'];
        $json_map_xj = $maps['json_map_xj'];
        file_put_contents(public_path('Airport/airports.json'), $json_map);
        file_put_contents(public_path('Airport/xj_airports.json'), $json_map_xj);
    }

    #[ArrayShape([
        'json_map'    => "string",
        'json_map_xj' => "string",
    ])]
    public function map($keywords = ''): array
    {
        $airports    = $this->airportRepo->listStations();
        $json_map    = [];
        $json_map_xj = [];

        foreach ($airports as $key => $airport) {
            $airport->initial_uy = $airport->initial_uy ?: '#';
            $city                = $this->areaRepo->findCityByCode($airport->citycode);
            $ad                  = $this->areaRepo->findByCode($airport->adcode);
            if ($ad && !$city) {
                $city = $this->areaRepo->findByCode($ad->getParentCode());
            }
            if (!$city || !$ad) {
                continue;
            }

            if (Str::contains($airport->name, " ")) {
                continue;
            }

            $iscity = isUseCity($airport->adname);
            $data   = [
                'abbr'        => $airport->abbr,
                'initial_uy'  => $airport->initial_uy,
                'initial'     => $airport->initial,
                'name'        => $airport->name,
                'name_uy'     => $airport->name_uy,
                'code'        => $airport->code,
                'city_code'   => $airport->city_code,
                'cityname'    => $airport->cityname,
                'cityname_uy' => $city->getAreaNameUy(),
                'adname'      => $airport->adname,
                'adname_uy'   => $ad->getAreaNameUy(),
                'citycode'    => $city->getAdCode(),
                'adcode'      => $airport->adcode,
                'iscity'      => $iscity,
            ];

            $json_map[$airport->initial_uy][] = $data;
            if ($airport->pcode == 650000) {
                $json_map_xj[$airport->initial_uy][] = $data;
            }

        }
        $json_map    = collect($json_map)->sortBy(fn($key, $v) => $v)->toJson(JSON_UNESCAPED_UNICODE);
        $json_map_xj = collect($json_map_xj)->sortBy(fn($key, $v) => $v)->toJson(JSON_UNESCAPED_UNICODE);
        return [
            'json_map'    => $json_map,
            'json_map_xj' => $json_map_xj,
        ];
    }
}